package com.hashcoding.eyes.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.hashcoding.eyes.R;
import com.hashcoding.eyes.data.Event.LocateEvent;
import com.hashcoding.eyes.module.manager.EventBusManager;
import com.hashcoding.eyes.module.manager.LocateManager;
import com.hashcoding.eyes.module.utils.Util;
import com.hashcoding.eyes.ui.view.SquareLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

public class AddActivity extends BaseActivity {

    private static final int REQUEST_IMAGE = 1001;

    @Bind(R.id.publish_image)
    ImageView mPublishImage;
    @Bind(R.id.publish_image_delete)
    ImageView mPublishImageDelete;
    @Bind(R.id.description)
    EditText mDescription;
    @Bind(R.id.location)
    TextView mLocation;
    @Bind(R.id.publish_image_layout)
    SquareLayout mPublishImageLayout;
    @Bind(R.id.title)
    EditText mTitle;

    //创建lcoationManager对象
    private LocationManager mLocationManager;
    private Uri mUri;
    private String mPosition;

    public static void launch(Context context) {
        context.startActivity(new Intent(context, AddActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        ButterKnife.bind(this);

        initView();
        EventBusManager.register(this);
        LocateManager.getInstance().startLocate();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBusManager.unregister(this);
    }

    private void initView() {
        mPublishImageLayout.setVisibility(View.VISIBLE);
        mPublishImageDelete.setVisibility(View.GONE);
    }

    @OnClick(R.id.publish_image)
    protected void onImageClick() {
        selectImage(REQUEST_IMAGE);
    }

    @OnClick(R.id.title_left_btn)
    protected void onTitleLeftBtn() {
        onBackPressed();
    }

    @OnClick(R.id.publish_image_delete)
    protected void onImageDeleteClick() {
        mPublishImage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        mPublishImage.setImageResource(R.drawable.camera);
        mPublishImage.setClickable(true);
        mPublishImageDelete.setVisibility(View.GONE);
        mUri = null;
    }

    @OnClick(R.id.add)
    protected void onAddPhotoClick() {
        if (mUri == null) {
            shortToast("请选择上传图片");
            return;
        }
        String name = mTitle.getText().toString();
        if (name == null || name.isEmpty()) {
            shortToast("请填写标题");
            return;
        }
        String description = mDescription.getText().toString();
        if (description == null || description.isEmpty()) {
            shortToast("请填写描述");
        }
        if (mPosition.isEmpty()) {
            shortToast("获取地址信息失败！");
        }
        showProgressDialog("上传中", false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismissProgressDialog();
                showMessageDialog("上传成功");
                onBackPressed();
            }
        }, 2000);
    }

    private void selectImage(int requestCode) {
        Util.gotoAlbumView(this, requestCode, true);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && data != null) {
            mUri = data.getData();
            switch (requestCode) {
                case REQUEST_IMAGE:
                    mPublishImage.setImageURI(mUri);
                    mPublishImage.setClickable(false);
                    mPublishImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    mPublishImageDelete.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEvent(LocateEvent event) {
        if (event.code == LocateEvent.CODE_SUCCESS) {
            AMapLocation location = LocateManager.getInstance().getLocation();
            mPosition = location.getProvince() + location.getCity() + location.getDistrict();
            mLocation.setText(mPosition);
        } else if (event.code == LocateEvent.CODE_FAILED) {
            shortToast("定位失败");
        }
    }
}
